using System;
using System.Linq;
using System.Runtime.Serialization;
using System.Data.Objects.DataClasses;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Data.Objects;
using System.Text;
using Acme.Northwind.EFDAL;
using nHydrate.EFCore.DataAccess;
using nHydrate.EFCore.EventArgs;
using System.Text.RegularExpressions;
using System.Linq.Expressions;
using System.Data.Linq;

namespace Acme.Northwind.EFDAL.Entity
{
	/// <summary>
	/// The collection to hold 'EmployeeTerritorie' entities
	/// </summary>
	[System.CodeDom.Compiler.GeneratedCode("nHydrateModelGenerator", "5.0.1.100")]
	[EdmEntityTypeAttribute(NamespaceName="Acme.Northwind.EFDAL.Entity", Name="EmployeeTerritorie")]
	[Serializable()]
	[DataContractAttribute(IsReference = true)]
	[nHydrate.EFCore.Attributes.FieldNameConstantsAttribute(typeof(Acme.Northwind.EFDAL.Entity.EmployeeTerritorie.FieldNameConstants))]
	[System.ComponentModel.DataAnnotations.MetadataType(typeof(Acme.Northwind.EFDAL.Interfaces.Entity.Metadata.EmployeeTerritorieMetadata))]
	[nHydrate.EFCore.Attributes.EntityMetadata("EmployeeTerritorie", false, true, true, true, "", true, false, false, "dbo")]
	public partial class EmployeeTerritorie : nHydrate.EFCore.DataAccess.NHEntityObject, nHydrate.EFCore.DataAccess.IBusinessObject, Acme.Northwind.EFDAL.IEntityWithContext, Acme.Northwind.EFDAL.Interfaces.Entity.IEmployeeTerritorie, System.ICloneable, nHydrate.EFCore.DataAccess.IAuditable, System.IEquatable<Acme.Northwind.EFDAL.Interfaces.Entity.IEmployeeTerritorie>
	{
		#region FieldNameConstants Enumeration

		/// <summary>
		/// Enumeration to define each property that maps to a database field for the 'EmployeeTerritorie' table.
		/// </summary>
		public enum FieldNameConstants
		{
			/// <summary>
			/// Field mapping for the 'EmployeeID' property
			/// </summary>
			[nHydrate.EFCore.Attributes.PrimaryKeyAttribute()]
			[System.ComponentModel.ReadOnlyAttribute(true)]
			[System.ComponentModel.Description("Field mapping for the 'EmployeeID' property")]
			EmployeeID,
			/// <summary>
			/// Field mapping for the 'TerritoryID' property
			/// </summary>
			[nHydrate.EFCore.Attributes.PrimaryKeyAttribute()]
			[System.ComponentModel.ReadOnlyAttribute(true)]
			[System.ComponentModel.Description("Field mapping for the 'TerritoryID' property")]
			TerritoryID,
			/// <summary>
			/// Field mapping for the 'CreatedBy' property
			/// </summary>
			[System.ComponentModel.Description("Field mapping for the 'CreatedBy' property")]
			CreatedBy,
			/// <summary>
			/// Field mapping for the 'CreatedDate' property
			/// </summary>
			[System.ComponentModel.Description("Field mapping for the 'CreatedDate' property")]
			CreatedDate,
			/// <summary>
			/// Field mapping for the 'ModifiedBy' property
			/// </summary>
			[System.ComponentModel.Description("Field mapping for the 'ModifiedBy' property")]
			ModifiedBy,
			/// <summary>
			/// Field mapping for the 'ModifiedDate' property
			/// </summary>
			[System.ComponentModel.Description("Field mapping for the 'ModifiedDate' property")]
			ModifiedDate,
		}
		#endregion

		#region Constructors

		/// <summary>
		/// Method called when an instance of this class is created
		/// </summary>
		partial void OnCreated();

		/// <summary>
		/// Initializes a new instance of the Acme.Northwind.EFDAL.Entity.EmployeeTerritorie class
		/// </summary>
		public EmployeeTerritorie()
		{
			this.OnCreated();
		}

		/// <summary>
		/// Initializes a new instance of the Acme.Northwind.EFDAL.Entity.EmployeeTerritorie class with a defined primary key
		/// </summary>
		public EmployeeTerritorie(int employeeID, string territoryID)
		{
			_employeeID = employeeID;
			_territoryID = territoryID;
			this.OnCreated();
		}

		#endregion

		#region Properties

		/// <summary>
		/// The property that maps back to the database 'EmployeeTerritories.EmployeeID' field
		/// </summary>
		/// <remarks>Field: [EmployeeTerritories].[EmployeeID], Not Nullable, Primary Key, Unique, Indexed</remarks>
		[System.ComponentModel.Browsable(true)]
		[EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
		[DataMemberAttribute()]
		[System.ComponentModel.DisplayName("EmployeeID")]
		[System.ComponentModel.DataAnnotations.Key()]
		[System.ComponentModel.ReadOnly(true)]
		public virtual int EmployeeID
		{
			get { return _employeeID; }
			set
			{
				ReportPropertyChanging("EmployeeID");
				_employeeID = value;
				_modifieddate = DateTime.Now;
				if ((this as IEntityWithContext).Context != null)
					_modifiedby = (this as IEntityWithContext).Context.ContextStartup.Modifer;
				ReportPropertyChanged("EmployeeID");
			}
		}

		/// <summary>
		/// The property that maps back to the database 'EmployeeTerritories.TerritoryID' field
		/// </summary>
		/// <remarks>Field: [EmployeeTerritories].[TerritoryID], Field Length: 20, Not Nullable, Primary Key, Unique, Indexed</remarks>
		[System.ComponentModel.Browsable(true)]
		[EdmScalarPropertyAttribute(EntityKeyProperty = true, IsNullable = false)]
		[DataMemberAttribute()]
		[System.ComponentModel.DisplayName("TerritoryID")]
		[System.ComponentModel.DataAnnotations.Key()]
		[System.ComponentModel.ReadOnly(true)]
		public virtual string TerritoryID
		{
			get { return _territoryID; }
			set
			{
				if ((value != null) && (value.Length > GetMaxLength(FieldNameConstants.TerritoryID))) throw new Exception(string.Format(GlobalValues.ERROR_DATA_TOO_BIG, value, "EmployeeTerritorie.TerritoryID", GetMaxLength(FieldNameConstants.TerritoryID)));
				ReportPropertyChanging("TerritoryID");
				_territoryID = value;
				_modifieddate = DateTime.Now;
				if ((this as IEntityWithContext).Context != null)
					_modifiedby = (this as IEntityWithContext).Context.ContextStartup.Modifer;
				ReportPropertyChanged("TerritoryID");
			}
		}

		/// <summary>
		/// The audit field for the 'Created By' parameter.
		/// </summary>
		[System.ComponentModel.Browsable(false)]
		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		public virtual string CreatedBy
		{
			get { return _createdby; }
			protected internal set
			{
				var eventArg = new nHydrate.EFCore.EventArgs.ChangingEventArgs<string>(value, "CreatedBy");
				//OnCreatedByChanging(eventArg);
				if (eventArg.Cancel) return;
				ReportPropertyChanging("CreatedBy");
				_createdby = eventArg.Value;
				ReportPropertyChanged("CreatedBy");
				//OnCreatedByChanged(eventArg);
			}
		}

		/// <summary>
		/// The internal reference variable for the 'createdby' property
		/// </summary>
		protected string _createdby;

		/// <summary>
		/// The audit field for the 'Created Date' parameter.
		/// </summary>
		[System.ComponentModel.Browsable(false)]
		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		public virtual DateTime? CreatedDate
		{
			get { return _createddate; }
			protected internal set
			{
				var eventArg = new nHydrate.EFCore.EventArgs.ChangingEventArgs<DateTime?>(value, "CreatedDate");
				//OnCreatedDateChanging(eventArg);
				if (eventArg.Cancel) return;
				ReportPropertyChanging("CreatedDate");
				_createddate = eventArg.Value;
				ReportPropertyChanged("CreatedDate");
				//OnCreatedDateChanged(eventArg);
			}
		}

		/// <summary>
		/// The internal reference variable for the 'createddate' property
		/// </summary>
		protected DateTime? _createddate;

		/// <summary>
		/// The audit field for the 'Modified By' parameter.
		/// </summary>
		[System.ComponentModel.Browsable(false)]
		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		public virtual string ModifiedBy
		{
			get { return _modifiedby; }
			protected internal set
			{
				var eventArg = new nHydrate.EFCore.EventArgs.ChangingEventArgs<string>(value, "ModifiedBy");
				//OnModifiedByChanging(eventArg);
				if (eventArg.Cancel) return;
				ReportPropertyChanging("ModifiedBy");
				_modifiedby = eventArg.Value;
				ReportPropertyChanged("ModifiedBy");
				//OnModifiedByChanged(eventArg);
			}
		}

		/// <summary>
		/// The internal reference variable for the 'modifiedby' property
		/// </summary>
		protected string _modifiedby;

		/// <summary>
		/// The audit field for the 'Modified Date' parameter.
		/// </summary>
		[System.ComponentModel.Browsable(false)]
		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		public virtual DateTime? ModifiedDate
		{
			get { return _modifieddate; }
			protected internal set
			{
				var eventArg = new nHydrate.EFCore.EventArgs.ChangingEventArgs<DateTime?>(value, "ModifiedDate");
				//OnModifiedDateChanging(eventArg);
				if (eventArg.Cancel) return;
				ReportPropertyChanging("ModifiedDate");
				_modifieddate = eventArg.Value;
				ReportPropertyChanged("ModifiedDate");
				//OnModifiedDateChanged(eventArg);
			}
		}

		/// <summary>
		/// The internal reference variable for the 'modifieddate' property
		/// </summary>
		protected DateTime? _modifieddate;

		/// <summary>
		/// The audit field for the 'Timestamp' parameter.
		/// </summary>
		[System.ComponentModel.Browsable(false)]
		[EdmScalarPropertyAttribute(EntityKeyProperty = false, IsNullable = true)]
		[DataMemberAttribute()]
		public virtual byte[] Timestamp
		{
			get { return _timestamp; }
			protected internal set
			{
				var eventArg = new nHydrate.EFCore.EventArgs.ChangingEventArgs<byte[]>(value, "Timestamp");
				//OnTimestampChanging(eventArg);
				if (eventArg.Cancel) return;
				ReportPropertyChanging("Timestamp");
				_timestamp = eventArg.Value;
				ReportPropertyChanged("Timestamp");
				//OnTimestampChanged(eventArg);
			}
		}

		/// <summary>
		/// The internal reference variable for the 'timestamp' property
		/// </summary>
		protected byte[] _timestamp;

		#endregion

		#region Events

		/// <summary>
		/// The internal reference variable for the 'EmployeeID' property
		/// </summary>
		protected int _employeeID;

		/// <summary>
		/// The internal reference variable for the 'TerritoryID' property
		/// </summary>
		protected string _territoryID;

		#endregion

		#region GetMaxLength

		/// <summary>
		/// Gets the maximum size of the field value.
		/// </summary>
		public static int GetMaxLength(FieldNameConstants field)
		{
			switch (field)
			{
				case FieldNameConstants.EmployeeID:
					return 0;
				case FieldNameConstants.TerritoryID:
					return 20;
			}
			return 0;
		}

		int nHydrate.EFCore.DataAccess.IReadOnlyBusinessObject.GetMaxLength(Enum field)
		{
			return GetMaxLength((FieldNameConstants)field);
		}

		#endregion

		#region GetFieldNameConstants

		System.Type nHydrate.EFCore.DataAccess.IReadOnlyBusinessObject.GetFieldNameConstants()
		{
			return typeof(FieldNameConstants);
		}

		#endregion

		#region GetFieldType

		/// <summary>
		/// Gets the system type of a field on this object
		/// </summary>
		public static System.Type GetFieldType(FieldNameConstants field)
		{
			if (field.GetType() != typeof(FieldNameConstants))
				throw new Exception("The '" + field.GetType().ReflectedType.ToString() + ".FieldNameConstants' value is not valid. The field parameter must be of type 'Acme.Northwind.EFDAL.Entity.EmployeeTerritorie.FieldNameConstants'.");

			var array = Enum.GetValues(typeof(FieldNameConstants));
			switch ((FieldNameConstants)field)
			{
				case FieldNameConstants.EmployeeID: return typeof(int);
				case FieldNameConstants.TerritoryID: return typeof(string);
			}
			return null;
		}

		System.Type nHydrate.EFCore.DataAccess.IReadOnlyBusinessObject.GetFieldType(Enum field)
		{
			if (field.GetType() != typeof(FieldNameConstants))
				throw new Exception("The '" + field.GetType().ReflectedType.ToString() + ".FieldNameConstants' value is not valid. The field parameter must be of type 'Acme.Northwind.EFDAL.Entity.EmployeeTerritorie.FieldNameConstants'.");

			return GetFieldType((Acme.Northwind.EFDAL.Entity.EmployeeTerritorie.FieldNameConstants)field);
		}

		#endregion

		#region Get/Set Value

		object nHydrate.EFCore.DataAccess.IReadOnlyBusinessObject.GetValue(System.Enum field)
		{
			return ((nHydrate.EFCore.DataAccess.IReadOnlyBusinessObject)this).GetValue(field, null);
		}

		object nHydrate.EFCore.DataAccess.IReadOnlyBusinessObject.GetValue(System.Enum field, object defaultValue)
		{
			if (field.GetType() != typeof(FieldNameConstants))
				throw new Exception("The '" + field.GetType().ReflectedType.ToString() + ".FieldNameConstants' value is not valid. The field parameter must be of type '" + this.GetType().ToString() + ".FieldNameConstants'.");
			return this.GetValue((FieldNameConstants)field, defaultValue);
		}

		void nHydrate.EFCore.DataAccess.IBusinessObject.SetValue(System.Enum field, object newValue)
		{
			if (field.GetType() != typeof(FieldNameConstants))
				throw new Exception("The '" + field.GetType().ReflectedType.ToString() + ".FieldNameConstants' value is not valid. The field parameter must be of type '" + this.GetType().ToString() + ".FieldNameConstants'.");
			this.SetValue((FieldNameConstants)field, newValue);
		}

		#endregion

		#region PrimaryKey

		/// <summary>
		/// Hold the primary key for this object
		/// </summary>
		protected nHydrate.EFCore.DataAccess.IPrimaryKey _primaryKey = null;
		nHydrate.EFCore.DataAccess.IPrimaryKey nHydrate.EFCore.DataAccess.IReadOnlyBusinessObject.PrimaryKey
		{
			get
			{
				if (_primaryKey == null) _primaryKey = new Acme.Northwind.EFDAL.Entity.EmployeeTerritoriePrimaryKey(this.EmployeeID, this.TerritoryID);
				return _primaryKey;
			}
		}

		#endregion

		#region IsParented

		/// <summary>
		/// Determines if this object is part of a collection or is detached
		/// </summary>
		[System.ComponentModel.Browsable(false)]
		public virtual bool IsParented
		{
		  get { return (this.EntityState != System.Data.EntityState.Detached); }
		}

		#endregion

		#region Clone

		/// <summary>
		/// Creates a shallow copy of this object of all simple properties
		/// </summary>
		/// <returns></returns>
		public virtual object Clone()
		{
			var newItem = new EmployeeTerritorie();
			return newItem;
		}

		#endregion

		#region IsEquivalent

		/// <summary>
		/// Determines if all of the fields for the specified object exactly matches the current object.
		/// </summary>
		/// <param name="item">The object to compare</param>
		public override bool IsEquivalent(nHydrate.EFCore.DataAccess.INHEntityObject item)
		{
			if (item == null) return false;
			if (!(item is Acme.Northwind.EFDAL.Interfaces.Entity.IEmployeeTerritorie)) return false;
			var o = item as Acme.Northwind.EFDAL.Interfaces.Entity.IEmployeeTerritorie;
			return (
				o.EmployeeID == this.EmployeeID &&
				o.TerritoryID == this.TerritoryID
				);
		}

		#endregion

		#region GetValue

		/// <summary>
		/// Gets the value of one of this object's properties.
		/// </summary>
		public object GetValue(FieldNameConstants field)
		{
			return GetValue(field, null);
		}

		/// <summary>
		/// Gets the value of one of this object's properties.
		/// </summary>
		public object GetValue(FieldNameConstants field, object defaultValue)
		{
			if (field == FieldNameConstants.EmployeeID)
				return this.EmployeeID;
			if (field == FieldNameConstants.TerritoryID)
				return this.TerritoryID;
			if (field == FieldNameConstants.CreatedBy)
				return ((this.CreatedBy == null) ? defaultValue : this.CreatedBy);
			if (field == FieldNameConstants.CreatedDate)
				return ((this.CreatedDate == null) ? defaultValue : this.CreatedDate);
			if (field == FieldNameConstants.ModifiedBy)
				return ((this.ModifiedBy == null) ? defaultValue : this.ModifiedBy);
			if (field == FieldNameConstants.ModifiedDate)
				return ((this.ModifiedDate == null) ? defaultValue : this.ModifiedDate);
			throw new Exception("Field '" + field.ToString() + "' not found!");
		}

		#endregion

		#region SetValue

		/// <summary>
		/// Assigns a value to a field on this object.
		/// </summary>
		/// <param name="field">The field to set</param>
		/// <param name="newValue">The new value to assign to the field</param>
		public void SetValue(FieldNameConstants field, object newValue)
		{
			SetValue(field, newValue, false);
		}

		/// <summary>
		/// Assigns a value to a field on this object.
		/// </summary>
		/// <param name="field">The field to set</param>
		/// <param name="newValue">The new value to assign to the field</param>
		/// <param name="fixLength">Determines if the length should be truncated if too long. When false, an error will be raised if data is too large to be assigned to the field.</param>
		public void SetValue(FieldNameConstants field, object newValue, bool fixLength)
		{
			if (field == FieldNameConstants.EmployeeID)
			{
				throw new Exception("Field '" + field.ToString() + "' is a primary key and cannot be set!");
			}
			else if (field == FieldNameConstants.TerritoryID)
			{
				throw new Exception("Field '" + field.ToString() + "' is a primary key and cannot be set!");
			}
			else
				throw new Exception("Field '" + field.ToString() + "' not found!");
		}

		#endregion

		#region Navigation Properties

		/// <summary>
		/// The navigation definition for walking [EmployeeTerritorie]->[Employee]
		/// Relationship Links: 
		/// [Employees.EmployeeID = EmployeeTerritories.EmployeeID] (Required)
		/// </summary>
		[XmlIgnoreAttribute()]
		[SoapIgnoreAttribute()]
		[EdmRelationshipNavigationPropertyAttribute("Acme.Northwind.EFDAL.Entity", "FK__EmployeeTerritorie_Employee", "Employee")]
		public virtual Employee Employee
		{
			get
			{
				var retval = EmployeeReference;
				if (!retval.IsLoaded && this.EntityState != System.Data.EntityState.Added && this.EntityState != System.Data.EntityState.Detached)
				{
					retval.Load();
				}
				return retval.Value;
			}
			set { ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Employee>("Acme.Northwind.EFDAL.Entity.FK__EmployeeTerritorie_Employee", "Employee").Value = value; }
		}

		/// <summary>
		/// The navigation reference definition for walking Employee->EmployeeTerritorie
		/// </summary>
		[BrowsableAttribute(false)]
		protected virtual EntityReference<Employee> EmployeeReference
		{
			get { return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Employee>("Acme.Northwind.EFDAL.Entity.FK__EmployeeTerritorie_Employee", "Employee"); }
			set
			{
				if ((value != null))
					((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<Employee>("Acme.Northwind.EFDAL.Entity.FK__EmployeeTerritorie_Employee", "Employee", value);
			}
		}

		#region IEmployeeTerritorie Interface

		Acme.Northwind.EFDAL.Interfaces.Entity.IEmployee Acme.Northwind.EFDAL.Interfaces.Entity.IEmployeeTerritorie.Employee
		{
			get { return this.Employee; }
			set { this.Employee = (Acme.Northwind.EFDAL.Entity.Employee)value; }
		}

		#endregion

		/// <summary>
		/// The navigation definition for walking [EmployeeTerritorie]->[Territory]
		/// Relationship Links: 
		/// [Territories.TerritoryID = EmployeeTerritories.TerritoryID] (Required)
		/// </summary>
		[XmlIgnoreAttribute()]
		[SoapIgnoreAttribute()]
		[EdmRelationshipNavigationPropertyAttribute("Acme.Northwind.EFDAL.Entity", "FK__EmployeeTerritorie_Territory", "Territory")]
		public virtual Territory Territory
		{
			get
			{
				var retval = TerritoryReference;
				if (!retval.IsLoaded && this.EntityState != System.Data.EntityState.Added && this.EntityState != System.Data.EntityState.Detached)
				{
					retval.Load();
				}
				return retval.Value;
			}
			set { ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Territory>("Acme.Northwind.EFDAL.Entity.FK__EmployeeTerritorie_Territory", "Territory").Value = value; }
		}

		/// <summary>
		/// The navigation reference definition for walking Territory->EmployeeTerritorie
		/// </summary>
		[BrowsableAttribute(false)]
		protected virtual EntityReference<Territory> TerritoryReference
		{
			get { return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Territory>("Acme.Northwind.EFDAL.Entity.FK__EmployeeTerritorie_Territory", "Territory"); }
			set
			{
				if ((value != null))
					((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<Territory>("Acme.Northwind.EFDAL.Entity.FK__EmployeeTerritorie_Territory", "Territory", value);
			}
		}

		#region IEmployeeTerritorie Interface

		Acme.Northwind.EFDAL.Interfaces.Entity.ITerritory Acme.Northwind.EFDAL.Interfaces.Entity.IEmployeeTerritorie.Territory
		{
			get { return this.Territory; }
			set { this.Territory = (Acme.Northwind.EFDAL.Entity.Territory)value; }
		}

		#endregion

		#endregion

		#region Static SQL Methods

		internal static string GetFieldAliasFromFieldNameSqlMapping(string alias)
		{
			alias = alias.Replace("[", string.Empty).Replace("]", string.Empty);
			switch (alias.ToLower())
			{
				case "employeeid": return "employeeid";
				case "territoryid": return "territoryid";
				case "createddate": return "createddate";
				case "createdby": return "createdby";
				case "modifieddate": return "modifieddate";
				case "modifiedby": return "modifiedby";
				case "timestamp": return "timestamp";
				default: throw new Exception("The select clause is not valid.");
			}
		}

		internal static string GetTableFromFieldAliasSqlMapping(string alias)
		{
			switch (alias.ToLower())
			{
				case "employeeid": return "EmployeeTerritories";
				case "territoryid": return "EmployeeTerritories";
				case "createdby": return "EmployeeTerritories";
				case "createddate": return "EmployeeTerritories";
				case "modifiedby": return "EmployeeTerritories";
				case "modifieddate": return "EmployeeTerritories";
				case "timestamp": return "EmployeeTerritories";
				default: throw new Exception("The select clause is not valid.");
			}
		}

		internal static string GetTableFromFieldNameSqlMapping(string field)
		{
			switch (field.ToLower())
			{
				case "employeeid": return "EmployeeTerritories";
				case "territoryid": return "EmployeeTerritories";
				case "createdby": return "EmployeeTerritories";
				case "createddate": return "EmployeeTerritories";
				case "modifiedby": return "EmployeeTerritories";
				case "modifieddate": return "EmployeeTerritories";
				case "timestamp": return "EmployeeTerritories";
				default: throw new Exception("The select clause is not valid.");
			}
		}

		internal static string GetRemappedLinqSql(string sql, string parentAlias, LinqSQLFromClauseCollection childTables)
		{
			sql = System.Text.RegularExpressions.Regex.Replace(sql, "\\[" + parentAlias + "\\]\\.\\[employeeid\\]", "[" + childTables.GetBaseAliasTable(parentAlias, "EmployeeTerritories") + "].[employeeid]", RegexOptions.IgnoreCase);
			sql = System.Text.RegularExpressions.Regex.Replace(sql, "\\[" + parentAlias + "\\]\\.\\[territoryid\\]", "[" + childTables.GetBaseAliasTable(parentAlias, "EmployeeTerritories") + "].[territoryid]", RegexOptions.IgnoreCase);
			sql = System.Text.RegularExpressions.Regex.Replace(sql, "\\[" + parentAlias + "\\]\\.\\[createdby\\]", "[" + childTables.GetBaseAliasTable(parentAlias, "EmployeeTerritories") + "].[createdby]", RegexOptions.IgnoreCase);
			sql = System.Text.RegularExpressions.Regex.Replace(sql, "\\[" + parentAlias + "\\]\\.\\[createddate\\]", "[" + childTables.GetBaseAliasTable(parentAlias, "EmployeeTerritories") + "].[createddate]", RegexOptions.IgnoreCase);
			sql = System.Text.RegularExpressions.Regex.Replace(sql, "\\[" + parentAlias + "\\]\\.\\[modifiedby\\]", "[" + childTables.GetBaseAliasTable(parentAlias, "EmployeeTerritories") + "].[modifiedby]", RegexOptions.IgnoreCase);
			sql = System.Text.RegularExpressions.Regex.Replace(sql, "\\[" + parentAlias + "\\]\\.\\[modifieddate\\]", "[" + childTables.GetBaseAliasTable(parentAlias, "EmployeeTerritories") + "].[modifieddate]", RegexOptions.IgnoreCase);
			sql = System.Text.RegularExpressions.Regex.Replace(sql, "\\[" + parentAlias + "\\]\\.\\[timestamp\\]", "[" + childTables.GetBaseAliasTable(parentAlias, "EmployeeTerritories") + "].[timestamp]", RegexOptions.IgnoreCase);
			return sql;
		}

		#endregion

		#region DeleteData

		/// <summary>
		/// Delete all records that match a where condition
		/// </summary>
		/// <param name="where">The expression that determines the records deleted</param>
		/// <returns>The number of rows deleted</returns>
		public static int DeleteData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where)
		{
			return DeleteData(where, new nHydrate.EFCore.DataAccess.QueryOptimizer());
		}

		/// <summary>
		/// Delete all records that match a where condition
		/// </summary>
		/// <param name="where">The expression that determines the records deleted</param>
		/// <param name="optimizer">The optimization object to use for running queries</param>
		/// <returns>The number of rows deleted</returns>
		public static int DeleteData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, nHydrate.EFCore.DataAccess.QueryOptimizer optimizer)
		{
			if (optimizer == null) throw new Exception("The 'optimizer' parameter cannot be null.");

			var dc = new DataContext(Acme.Northwind.EFDAL.DBHelper.GetConnection());
			var  template = dc.GetTable<Acme.Northwind.EFDAL.EmployeeTerritorieQuery>();
			var cmd = nHydrate.EFCore.DataAccess.BusinessEntityQuery.GetCommand<Acme.Northwind.EFDAL.EmployeeTerritorieQuery>(dc, template, where);
			cmd.CommandTimeout = 30;

			var parser = LinqSQLParser.Create(cmd.CommandText, LinqSQLParser.ObjectTypeConstants.Table);
			string sql = "SELECT [t0].[EmployeeID], [t0].[TerritoryID] INTO #t\r\n";
			sql += parser.GetFromClause(optimizer) + "\r\n";
			sql += parser.GetWhereClause();
			sql += "\r\n";

			var noLock = string.Empty;
			noLock = (optimizer.NoLocking ? "WITH (NOLOCK) " : string.Empty);
			sql += "DELETE [EmployeeTerritories] FROM [dbo].[EmployeeTerritories] " + noLock + "INNER JOIN #t ON [dbo].[EmployeeTerritories].[EmployeeID] = #t.[EmployeeID] AND [dbo].[EmployeeTerritories].[TerritoryID] = #t.[TerritoryID]\r\n";
			sql += ";select @@rowcount";
			cmd.CommandText = sql;
			dc.Connection.Open();
			var startTime = DateTime.Now;
			object p = cmd.ExecuteScalar();
			var endTime = DateTime.Now;
			optimizer.TotalMilliseconds = (long)endTime.Subtract(startTime).TotalMilliseconds;
			dc.Connection.Close();
			return (int)p;
		}

		#endregion

		#region UpdateData

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, int>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, int newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, int>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, int?>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, int? newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, int?>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, Single>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, Single newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, Single>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, Single?>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, Single? newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, Single?>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, decimal>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, decimal newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, decimal>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, decimal?>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, decimal? newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, decimal?>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, double>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, double newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, double>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, double?>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, double? newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, double?>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, short>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, short newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, short>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, short?>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, short? newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, short?>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, string>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, string newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, string>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, DateTime>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, DateTime newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, DateTime>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, DateTime?>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, DateTime? newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, DateTime?>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, bool newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool?>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, bool? newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool?>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, Guid>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, Guid newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, Guid>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, Guid?>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, Guid? newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, Guid?>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		/// <summary>
		/// Update the specified field that matches the Where expression with the new data value
		/// </summary>
		/// <param name="select">The field to update</param>
		/// <param name="where">The expression that determines the records selected</param>
		/// <param name="newValue">The new value to set the specified field in all matching records</param>
		/// <returns>The number of records affected</returns>
		public static int UpdateData(Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, byte[]>> select, Expression<Func<Acme.Northwind.EFDAL.EmployeeTerritorieQuery, bool>> where, byte[] newValue)
		{
			return BusinessObjectQuery<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, Acme.Northwind.EFDAL.EmployeeTerritorieQuery, byte[]>.UpdateData(select, where, newValue, "EmployeeTerritories", GetDatabaseFieldName, true);
		}

		#endregion

		#region GetDatabaseFieldName

		/// <summary>
		/// Returns the actual database name of the specified field.
		/// </summary>
		internal static string GetDatabaseFieldName(EmployeeTerritorie.FieldNameConstants field)
		{
			return GetDatabaseFieldName(field.ToString());
		}

		/// <summary>
		/// Returns the actual database name of the specified field.
		/// </summary>
		internal static string GetDatabaseFieldName(string field)
		{
			switch (field)
			{
				case "EmployeeID": return "EmployeeID";
				case "TerritoryID": return "TerritoryID";
				case "CreatedBy": return "CreatedBy";
				case "CreatedDate": return "CreatedDate";
				case "ModifiedBy": return "ModifiedBy";
				case "ModifiedDate": return "ModifiedDate";
				case "Timestamp": return "Timestamp";
			}
			return string.Empty;
		}

		#endregion

		#region Context

		NorthwindEntities IEntityWithContext.Context
		{
			get { return _internalContext; }
			set { _internalContext = value; }
		}
		private NorthwindEntities _internalContext = null;

		#endregion

		#region Auditing
		string nHydrate.EFCore.DataAccess.IAuditable.CreatedBy
		{
			get { return this.CreatedBy; }
		}

		System.DateTime? nHydrate.EFCore.DataAccess.IAuditable.CreatedDate
		{
			get { return this.CreatedDate; }
		}

		bool nHydrate.EFCore.DataAccess.IAuditable.IsCreateAuditImplemented
		{
			get { return true; }
		}

		bool nHydrate.EFCore.DataAccess.IAuditable.IsModifyAuditImplemented
		{
			get { return true; }
		}

		bool nHydrate.EFCore.DataAccess.IAuditable.IsTimestampAuditImplemented
		{
			get { return true; }
		}

		string nHydrate.EFCore.DataAccess.IAuditable.ModifiedBy
		{
			get { return this.ModifiedBy; }
		}

		System.DateTime? nHydrate.EFCore.DataAccess.IAuditable.ModifiedDate
		{
			get { return this.ModifiedDate; }
		}

		byte[] nHydrate.EFCore.DataAccess.IAuditable.TimeStamp
		{
			get { return this.Timestamp; }
		}

		internal virtual void ResetModifiedBy(string modifier)
		{
			if (this.ModifiedBy != modifier)
				this.ModifiedBy = modifier;
		}

		internal virtual void ResetCreatedBy(string modifier)
		{
			if (this.CreatedBy != modifier)
				this.CreatedBy = modifier;
			this.ResetModifiedBy(modifier);
		}
		#endregion

		#region Static Methods

		/// <summary>
		/// Select a single element by its primary key value
		/// </summary>
		public static Acme.Northwind.EFDAL.Entity.EmployeeTerritorie SelectByPK(int employeeID, string territoryID)
		{
			using (var context = new Acme.Northwind.EFDAL.NorthwindEntities())
			{
				return context.EmployeeTerritorie.FirstOrDefault(x => x.EmployeeID == employeeID && x.TerritoryID == territoryID);
			}
		}

		/// <summary>
		/// Returns a list of distinct values based on a predicate
		/// </summary>
		public static IEnumerable<T> GetDistinct<T>(System.Linq.Expressions.Expression<Func<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, T>> select, System.Linq.Expressions.Expression<Func<EmployeeTerritorie, bool>> predicate)
		{
			using (var context = new Acme.Northwind.EFDAL.NorthwindEntities())
			{
				return context.EmployeeTerritorie.Where(predicate).Select(select).Distinct().ToList();
			}
		}

		/// <summary>
		/// Returns a count of items based on a predicate
		/// </summary>
		public static int GetCount(System.Linq.Expressions.Expression<Func<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, bool>> predicate)
		{
			using (var context = new Acme.Northwind.EFDAL.NorthwindEntities())
			{
				return context.EmployeeTerritorie.Count(predicate);
			}
		}

		/// <summary>
		/// Returns a maximum property value of based on a predicate
		/// </summary>
		public static T GetMax<T>(System.Linq.Expressions.Expression<Func<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, T>> select, System.Linq.Expressions.Expression<Func<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, bool>> predicate)
		{
			using (var context = new Acme.Northwind.EFDAL.NorthwindEntities())
			{
				return context.EmployeeTerritorie.Where(predicate).Select(select).Max();
			}
		}

		/// <summary>
		/// Returns a minimum property value of based on a predicate
		/// </summary>
		public static T GetMin<T>(System.Linq.Expressions.Expression<Func<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, T>> select, System.Linq.Expressions.Expression<Func<Acme.Northwind.EFDAL.Entity.EmployeeTerritorie, bool>> predicate)
		{
			using (var context = new Acme.Northwind.EFDAL.NorthwindEntities())
			{
				return context.EmployeeTerritorie.Where(predicate).Select(select).Min();
			}
		}

		/// <summary>
		/// Creates and returns a metadata object for an entity type
		/// </summary>
		/// <returns>A metadata object for the entity types in this assembly</returns>
		public static Acme.Northwind.EFDAL.Interfaces.Entity.Metadata.EmployeeTerritorieMetadata GetMetadata()
		{
			var a = typeof(EmployeeTerritorie).GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.MetadataTypeAttribute), true).FirstOrDefault();
			if (a == null) return null;
			var t = ((System.ComponentModel.DataAnnotations.MetadataTypeAttribute)a).MetadataClassType;
			if (t == null) return null;
			return Activator.CreateInstance(t) as nHydrate.EFCore.DataAccess.IMetadata as Acme.Northwind.EFDAL.Interfaces.Entity.Metadata.EmployeeTerritorieMetadata;
		}

		#endregion

		#region Equals
		bool System.IEquatable<Acme.Northwind.EFDAL.Interfaces.Entity.IEmployeeTerritorie>.Equals(Acme.Northwind.EFDAL.Interfaces.Entity.IEmployeeTerritorie other)
		{
			return this.IsEquivalent(other);
		}
		#endregion

		#region IDataErrorInfo
		/// <summary>
		/// 
		/// </summary>
		string System.ComponentModel.IDataErrorInfo.Error
		{
			get { return this.GetObjectDataErrorInfo(); }
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="columnName"></param>
		/// <returns></returns>
		string System.ComponentModel.IDataErrorInfo.this[string columnName]
		{
			get
			{
				if (string.IsNullOrEmpty(columnName))
					return string.Empty;

				var retval = GetObjectPropertyDataErrorInfo(columnName);
				if (string.IsNullOrEmpty(retval))
				{
					switch (columnName.ToLower())
					{
						case "territoryid":
							if (string.IsNullOrEmpty(this.TerritoryID) || string.IsNullOrEmpty(this.TerritoryID.Trim()))
								retval = "TerritoryID is required!";
							break;
						default:
							break;
					}
				}
				return retval;
			}
		}
		#endregion

	}

	partial class EmployeeTerritorie : nHydrate.EFCore.DataAccess.ICreatedAudit, nHydrate.EFCore.DataAccess.IModifiedAudit, nHydrate.EFCore.DataAccess.IConcurrencyAudit
	{
		#region ICreatedAudit Members

		string ICreatedAudit.CreatedBy
		{
			get { return this.CreatedBy; }
		}

		DateTime? ICreatedAudit.CreatedDate
		{
			get { return this.CreatedDate; }
		}

		#endregion

		#region IModifiedAudit Members

		string IModifiedAudit.ModifiedBy
		{
			get { return this.ModifiedBy; }
		}

		DateTime? IModifiedAudit.ModifiedDate
		{
			get { return this.ModifiedDate; }
		}

		#endregion

		#region IConcurrencyAudit Members

		byte[] IConcurrencyAudit.TimeStamp
		{
			get { return this.Timestamp; }
		}

		#endregion

	}

}

